<template>
    <div>
         <!-- 面包屑导航区域 -->
        <div class="bread_box">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>消息管理</el-breadcrumb-item>
                <el-breadcrumb-item>投诉列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <!-- 卡片视图区域 -->
        <el-card>
            <!-- 搜索与添加区域 这个searchFrom写错了字母-->
            <el-form class="search_from"
            :model="searchFrom"
            ref="searchFromRef"
            size="small" :inline="isinline" label-width="70px">
                <el-form-item label="文章名称">
                    <el-input v-model="searchFrom.name" placeholder="请输入文章名称"></el-input>
                </el-form-item>

                <el-form-item label="状态" prop="flag">
                    <el-select v-model="searchFrom.flag" placeholder="处理状态">
                        <el-option label="已处理" value="true"></el-option>
                        <el-option label="未处理" value="false"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="投诉时间" prop="time">
                    <el-date-picker
                        v-model="searchFrom.time"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="searchComplaint()">搜索</el-button>
                    <el-button  icon="el-icon-refresh" size="mini" @click="resetSearchFrom()">重置</el-button>
                </el-form-item>

            </el-form>

            <div>
                <el-button :disabled="!mulDeal" type="success" icon="el-icon-edit" size="mini" @click="handleComplaintMul()">处理</el-button>
                <el-button :disabled="!mulDeal" type="danger" icon="el-icon-delete" size="mini" @click="delComplaintMul()">删除</el-button>
                <el-popover
                  placement="top"
                  width="250"
                  v-model="exlVisible"
                  class="exl">
                  <p>请选择你所需要导出的类型！</p>
                  <div style="text-align: right; margin: 0">
                    <el-button size="mini"  @click="exlAll()">全部</el-button>
                    <el-button type="primary" size="mini" @click="exlSelect()">选中</el-button>
                  </div>
                  <el-button slot="reference" type="warning" icon="el-icon-download" size="mini">导出</el-button>
                </el-popover>
            </div>

            <el-table
                ref="multipleTable"
                :data="complaintList"
                tooltip-effect="dark"
                style="width: 100%"
                 @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column prop="fromId" label="投诉人Id" ></el-table-column>
                <el-table-column prop="toId" label="被投诉人Id"> </el-table-column>
                <!-- <el-table-column prop="articleName" label="文章名称"> </el-table-column> -->
                <el-table-column label="处理">
                    <template slot-scope="scope">
                        <!-- {{scope.row}} -->
                        <el-switch v-model="scope.row.flag" @change="complaintStateChanged(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column prop="content" label="投诉内容" :show-overflow-tooltip="true"> </el-table-column>
                <el-table-column prop="created" label="创建时间"></el-table-column>
                <!-- <el-table-column prop="result" label="处理结果"> </el-table-column> -->
                <!-- <el-table-column prop="resultTime" label="处理时间"> </el-table-column> -->

                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="showHandleDetail(scope.row)"><i class="el-icon-view"></i><span>详情</span></el-button>
                        <el-button type="text" size="mini" @click="toHandleComplaint(scope.row)"><i class="el-icon-edit"></i><span>处理</span></el-button>
                        <el-button type="text" size="mini" @click="delComplaint(scope.row)"><i class="el-icon-delete"></i><span>删除</span></el-button>
                    </template>
                </el-table-column>
            </el-table>

             <!-- 分页区域 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageInfo.cur"
                :page-sizes="[4, 8, 10, 20]"
                :page-size="pageInfo.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                background>
            </el-pagination>

        </el-card>

        <!-- 投诉处理对话框 -->
        <el-dialog
            title="投诉处理"
            :visible.sync="complaintDialogVisible"
            width="40%"
            @close="complaintDialogClose()">
            <!-- 处理表单 -->
            <el-form label-width="80px"
                ref="handleComplaintFormRef"
                :model="handleComplaintForm"
                :rules="handleComplaintFormRules"
                label-position="top"
                class="complaintForm">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="投诉Id" prop="id">
                            <el-input disabled v-model="handleComplaintForm.id"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                 <el-form-item label="处理结果" prop="result">
                    <el-input v-model="handleComplaintForm.result"
                    type="textarea" rows="3" :autosize="{ minRows: 3, maxRows: 5}"></el-input>
                </el-form-item>
                <el-form-item label="处理状态" prop="flag">
                    <el-switch v-model="handleComplaintForm.flag"></el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="complaintDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleComplaint()">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 投诉处理详细对话框 -->
        <el-dialog
            title="投诉处理详细"
            :visible.sync="handlecomplaintDialogVisible"
            width="40%"
            :modal="handleDetailForm">
            <el-form label-width="100px"

                class="complaintForm">
                 <el-form-item label="投诉人Id：">
                    {{handleDetailForm.fromId}}
                </el-form-item>
                 <el-form-item label="投诉说明：" >
                    {{handleDetailForm.content}}
                </el-form-item>
                <el-form-item label="处理状态：">
                    <span v-if="handleDetailForm.flag">已处理</span>
                    <span v-else>未处理</span>
                </el-form-item>
                <el-form-item label="处理结果：">
                    {{handleDetailForm.result}}
                </el-form-item>
                <el-form-item label="处理日期：" >
                    {{handleDetailForm.resultTime}}
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="handlecomplaintDialogVisible = false">关 闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单是否inline
      isinline: true,
      // 分页参数
      pageInfo: {
        cur: 1,
        size: 5,
        flag: '',
        leftTime: '',
        rightTime: ''
      },
      // 列表数目总数
      total: 0,
      // 投诉列表
      complaintList: [],
      // 选中列表的原始数据
      mulList: [],
      // 选择列表获取的列表(导出excel用)
      multipleSelection: [],
      // 投诉对话框显示与隐藏
      complaintDialogVisible: false,
      // 投诉处理详细对话框显示与隐藏
      handlecomplaintDialogVisible: false,
      // 导出exl选中显示与隐藏
      exlVisible: false,
      // 是否触发批处理按钮
      mulDeal: false,
      // 搜索条件表单
      searchFrom: {
        name: '',
        flag: '', // 处理状态
        time: ''
        // leftTime: '',
        // rightTime: ''
      },
      // 处理投诉表单
      handleComplaintForm: {
        id: '', // 投诉id
        result: '', // 处理结果
        flag: '' // 处理状态
      },
      // 处理投诉表单验证
      handleComplaintFormRules: {
        id: [{ required: true, message: '投诉id不能为空', trigger: 'blur' }],
        result: [{ required: true, message: '请输入处理结果', trigger: 'blur' }],
        flag: [{ required: true }]
      },
      // 投诉详细结果表单
      handleDetailForm: {
        fromId: '', // 发起投诉人id
        content: '', // 投诉原因说明
        flag: '', // 处理状态
        result: '', // 处理结果
        resultTime: '' // 处理日期
      }
    }
  },
  methods: {
    // 获取投诉列表
    async getComplaintList () {
      const { data: res } = await this.$http.post('/complaint/adminSelect', this.pageInfo)
      console.log('获取投诉列表', res)
      if (res.code !== 200) return
      this.total = res.data.total
      this.complaintList = res.data.list
      // this.getComplaintListExl(this.complaintList)
    },
    // 监听 size 改变的事件
    handleSizeChange (newSize) {
    //   console.log(newSize)
      this.pageInfo.size = newSize
      this.getComplaintList()
    },
    // 监听 cur 页码值 改变的事件
    handleCurrentChange (newCur) {
    //   console.log(newCur)
      this.pageInfo.cur = newCur
      this.getComplaintList()
    },
    // 打开投诉 Dialog 赋值参数
    toHandleComplaint (info) {
      this.complaintDialogVisible = true
      this.handleComplaintForm.id = info.id
      this.handleComplaintForm.flag = info.flag
      this.handleComplaintForm.result = info.result
    },
    // 监听 switch 开关状态, 默认处理投诉
    async complaintStateChanged (info) {
      this.handleComplaintForm.id = info.id
      this.handleComplaintForm.flag = info.flag
      this.handleComplaintForm.result = '管理员已读。'
      const { data: res } = await this.$http.post('/complaint/handle', this.handleComplaintForm)
      console.log('默认投诉处理', res)
      if (res.code !== 200) {
        info.flag = !info.flag
        return this.$message.error('处理投诉失败！')
      }
      this.getComplaintList()
    },
    // 批处理投诉，默认处理方式
    async handleComplaintMul () {
      console.log('批量处理', this.mulList)
      for (var i = 0; i < this.mulList.length; i++) {
        // console.log(this.mulList[i].id)
        this.handleComplaintForm.id = this.mulList[i].id
        this.handleComplaintForm.flag = !this.mulList[i].flag
        if (this.handleComplaintForm.flag) {
          this.handleComplaintForm.result = '管理员已读。'
        } else {
          this.handleComplaintForm.result = '管理员未读。'
        }
        const { data: res } = await this.$http.post('/complaint/handle', this.handleComplaintForm)
        if (res.code !== 200) {
          return this.$message.error('处理投诉失败！')
        }
      }
      this.$message.success('投诉批处理完成！')
      this.getComplaintList()
    },
    // 处理投诉，自定义回复处理方式
    handleComplaint () {
      // 投诉表单预校验
      this.$refs.handleComplaintFormRef.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post('/complaint/handle', this.handleComplaintForm)
          console.log('投诉处理', res)
          if (res.code !== 200) return this.$message.error('处理投诉失败！')
          this.$message.success('处理投诉成功！')
          this.getComplaintList()
          this.complaintDialogVisible = false
        }
      })
    },
    // 批处理删除投诉
    delComplaintMul () {
      var len = this.mulList.length
      this.$confirm('此操作将永久删除这 ' + len + ' 条投诉, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '批量删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 删除投诉
    delComplaint (info) {
      this.$confirm('此操作将永久删除该投诉, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 处理投诉 Dialog 关闭时清空表单
    complaintDialogClose () {
      this.$refs.handleComplaintFormRef.resetFields()
    },
    // 展示投诉详细结果
    showHandleDetail (info) {
      this.handlecomplaintDialogVisible = true
      // 为详细表单赋值
      this.handleDetailForm.fromId = info.fromId
      this.handleDetailForm.content = info.content
      this.handleDetailForm.flag = info.flag
      this.handleDetailForm.result = info.result
      this.handleDetailForm.resultTime = info.resultTime
    },

    // 根据搜索条件表单数据，查询投诉列表
    searchComplaint () {
      this.pageInfo.flag = this.searchFrom.flag
      this.pageInfo.leftTime = this.searchFrom.time[0]
      this.pageInfo.rightTime = this.searchFrom.time[1]
      // console.log('时间', this.searchFrom.time)
      // console.log(this.pageInfo.leftTime)
      // console.log(this.pageInfo.rightTime)
      this.getComplaintList()
    },
    // 重置搜索条件表单数据，获取所有投诉列表
    resetSearchFrom () {
      this.$refs.searchFromRef.resetFields() // 使用这个方法要添加prop，不然没效果
      this.pageInfo.flag = ''
      this.pageInfo.leftTime = this.pageInfo.rightTime = ''
      this.getComplaintList()
    },
    // 返回截取的部分字段
    getComplaintListExl (exlList) {
      var res = exlList.map(({ fromId, toId, content, created, flag, result, resultTime }) => ({ fromId, toId, content, created, flag, result, resultTime }))
      // console.log('截取', res)
      return res
    },
    // 多选后，获得数据
    handleSelectionChange (val) {
      this.mulList = val
      // 根据是否选中条目而启用批处理按钮
      if (this.mulList.length > 0) {
        this.mulDeal = true
      } else {
        this.mulDeal = false
      }
      this.multipleSelection = this.getComplaintListExl(val)
      console.log('获取多选数据', this.multipleSelection)
    },
    // 导出选中
    exlSelect () {
      if (this.multipleSelection.length <= 0) {
        return this.$message.warning('尚未选中数据！')
      }
      // console.log('导出选中', this.multipleSelection)
      this.export2Excel()
      this.exlVisible = false
      this.$message.success('正在导出excel，请耐心等待！')
    },
    // 导出全部
    async exlAll () {
      var queryInfo = { cur: 1, size: this.total, flag: '', leftTime: '', rightTime: '' }
      const { data: res } = await this.$http.post('/complaint/adminSelect', queryInfo)
      this.multipleSelection = this.getComplaintListExl(res.data.list)
      // console.log('导出全部', this.multipleSelection)
      this.export2Excel()
      this.exlVisible = false

      this.$message.success('正在导出excel，请耐心等待！')
    },
    // --------导出excel功能------------------------
    formatJson (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    export2Excel () {
      // console.log('导出中-----------')
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../../vendor/Export2Excel.js')
        const tHeader = ['投诉人Id', '被投诉人Id', '投诉原因', '投诉时间', '是否处理', '处理反馈内容', '处理时间']
        const filterVal = ['fromId', 'toId', 'content', 'created', 'flag', 'result', 'resultTime']
        const list = this.multipleSelection
        const data = this.formatJson(filterVal, list)
        export_json_to_excel(tHeader, data, '用户投诉列表')
      })
    }
    // ----------------------------------------------

  },
  created () {
    this.getComplaintList()
  }
}
</script>

<style lang="less" scoped>
.exl{
  margin-left: 10px;
}

</style>

<style lang="less">
    .complaintForm{
        // 改变el-form的label
        .el-form-item{
            .el-form-item__label{
                font-weight: 700 !important;
                line-height: 24px !important;
            }
            .el-form-item__content{
                line-height: 24px !important;
            }
        }
    }
</style>
